//
//  JTextView.m
//  PigMarket
//
//  Created by mac on 2019/3/18.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "JTextView.h"


@interface JTextView ()<UITextViewDelegate>
/**
 bgView
 */
@property (nonatomic , strong) UIView *inputBgView;
/**
 titleLab
 */
@property (nonatomic , strong) UILabel *titleLab;
/**
 placeholderLab
 */
@property (nonatomic , strong) UILabel *placeholderLab;

/**
 countLab
 */
@property (nonatomic , strong) UILabel *countLab;

/**
 limitWord
 */
@property (nonatomic , assign) NSInteger limitMaxWord;
/**
 currentWord
 */
@property (nonatomic , assign) NSInteger currentWord;

@end
@implementation JTextView

#pragma mark - init method

- (instancetype)initWithTextViewType:(TextViewType)type title:(NSString *)title placeholderLabStr:(NSString *)placeholderStr limitMaxWord:(NSInteger)limitMaxWord confirmBtnTitle:(NSString *)ConfirmBtnTitle{
        self = [super init];
        if (self) {
            [self initView];
            [self initLayout];
            [self initDataWithTextViewType:type title:title placeholderLabStr:placeholderStr limitMaxWord:limitMaxWord confirmBtnTitle:ConfirmBtnTitle];
        }
        return self;
}
- (void)initView {
    // 添加子控件
    [self addSubview:self.inputBgView];
    [self addSubview:self.titleLab];
    [self.inputBgView addSubview:self.remarkTextView];
    [self.inputBgView addSubview:self.countLab];
    [self addSubview:self.confirmBtn];
}

- (void)initLayout {
    // 布局子控件
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.top.mas_offset(15);
        make.width.mas_offset(SCREEN_WIDTH - 20);
    }];
    
    [self.inputBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(15);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(185);
    }];
    [self.remarkTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(150);
    }];
    [self.countLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(self.remarkTextView.mas_bottom).offset(12);
        make.width.mas_equalTo(200);
    }];
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(self.inputBgView.mas_bottom).offset(60);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(48);
        make.bottom.mas_equalTo(0);
    }];
}
- (void)initDataWithTextViewType:(TextViewType)type title:(NSString *)title placeholderLabStr:(NSString *)placeholderStr limitMaxWord:(NSInteger)limitMaxWord confirmBtnTitle:(NSString *)confirmBtnTitle{
    switch (type) {
        case TextViewTypePersonalRemark://个性签名
            break;
            
        default:
            break;
    }
    self.titleLab.text = title;
    self.limitMaxWord = limitMaxWord;
    self.remarkTextView.placeholder = placeholderStr;
    self.countLab.text = [NSString stringWithFormat:@"0/%ld",(long)limitMaxWord];
    [self.confirmBtn setTitle:confirmBtnTitle forState:UIControlStateNormal];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (textView.text.length > self.limitMaxWord) {
        textView.text = [textView.text substringToIndex:self.limitMaxWord];
        return NO;
    }
    if ([text isEqualToString:@"\n"]) {
        return NO;
    }
    NSString *currentStr = [NSString stringWithFormat:@"%@%@",textView.text, text];
    if (currentStr.length > self.limitMaxWord) {
        NSRange rangeIndex = [currentStr rangeOfComposedCharacterSequenceAtIndex:self.limitMaxWord];
        if (rangeIndex.length == 1) {
            //字数超限
            textView.text = [textView.text substringToIndex:self.limitMaxWord];
        }else {
            NSRange range = [currentStr rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, self.limitMaxWord)];
            textView.text = [currentStr substringWithRange:range];
        }
        return NO;
        
    }
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView {
    self.currentWord = textView.text.length;
    self.countLab.text = [NSString stringWithFormat:@"%ld/%ld",self.currentWord, (long)self.limitMaxWord];
}
#pragma mark - getter and setter
- (UIView *)inputBgView {
    if (!_inputBgView) {
        _inputBgView = [[UIView alloc] init];
        _inputBgView.backgroundColor = [UIColor globalBackgroundColor];
    }
    return _inputBgView;
}
- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = [UIColor textBlackColor];
        _titleLab.font = [UIFont largerFont];
    }
    return _titleLab;
}
- (MyTextView *)remarkTextView {
    if (!_remarkTextView) {
        _remarkTextView = [[MyTextView alloc] init];
        _remarkTextView.textColor = [UIColor textBlackColor];
        _remarkTextView.delegate = self;
        _remarkTextView.backgroundColor = [UIColor globalBackgroundColor];
        _remarkTextView.placeholderColor = [UIColor colorWithHexString:@"#999999"];
    }
    return _remarkTextView;
}
- (UILabel *)countLab {
    if (!_countLab) {
        _countLab = [[UILabel alloc] init];
        _countLab.textColor = [UIColor colorWithHexString:@"#CCCCCC"];
        _countLab.textAlignment = NSTextAlignmentRight;
        _countLab.font = [UIFont middleFont];
    }
    return _countLab;
}
- (UILabel *)placeholderLab {
    if (!_placeholderLab) {
        _placeholderLab = [[UILabel alloc] init];
        _placeholderLab.textColor = [UIColor colorWithHexString:@"#999999"];
        _placeholderLab.font = [UIFont smallFont];
    }
    return _placeholderLab;
}
- (UIButton *)confirmBtn {
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmBtn setBackgroundImage:[UIImage imageNamed:@"login_loginbtn_default"] forState:UIControlStateNormal];
    }
    return _confirmBtn;
}
@end
